Ext.define('CM.controller.Customers', {
	extend : 'Ext.app.Controller',

	models : [ 'Customer' ],
	stores : [ 'Customers' ],
	views : [ 'customer.Grid', 'customer.Edit', 'customer.Search' ],

	refs : [ {
		ref : 'customerGrid',
		selector : 'customergrid'
	}, {
		ref : 'customerEdit',
		selector : 'customeredit'
	}, {
		ref : 'customerEditForm',
		selector : 'customeredit form'
	}, {
		ref : 'customerSearch',
		selector : 'customersearch'
	} ],

	init : function() {
		console.log(this.$className, 'init');

		this.control({
			'customergrid' : {
				render : this.onGridRendered
			},
			'customergrid' : {
				itemdblclick : this.openEditFormToUpdate
			},
			'customergrid button[action=new]' : {
				click : this.openEditFormToCreate
			},
			'customergrid button[action=delete]' : {
				click : this.confirmDeleteCustomer
			},
			'customeredit button[action=cancel]' : {
				click : this.closeEditForm
			},
			'customeredit button[action=save]' : {
				click : this.saveCustomer
			},
			'customersearch button[action=clear]' : {
				click : this.clearSearchForm
			},
			'customersearch button[action=search]' : {
				click : this.searchCustomers
			}
		});
	},
	
	startEdit : function() {
		console.log(this.$className, 'startEdit');
		
		this.getCustomersStore().insert(0, new Customer());
		rowEditing.startEdit(0, 0);
	},

	onGridRendered : function() {
		console.log(this.$className, 'onGridRendered');
	},

	openEditFormToUpdate : function(grid, record) {
		console.log(this.$className, 'openEditFormToUpdate', record.get('id'));
		this.openEditForm(record);

	},

	openEditFormToCreate : function(button) {
		console.log(this.$className, 'openEditFormToCreate');
		this.openEditForm();
	},

	openEditForm : function(record) {
		var view = this.getCustomerEdit();
		if (!view) {
			view = Ext.create('widget.customeredit');
		}
		if (record) {
			view.edition = true;
			view.down('form').loadRecord(record);
		}
	},

	closeEditForm : function(button) {
		console.log(this.$className, 'closeEditForm');
		this.getCustomerEdit().close();
	},

	clearSearchForm : function(button) {
		console.log(this.$className, 'clearForm');
		this.getCustomerSearch().getForm().reset();
	},

	confirmDeleteCustomer : function(button) {
		var me = this;
		var grid = this.getCustomerGrid();
		var records = grid.getSelectionModel().getSelection();
		console.log(this.$className, 'deleteCustomer', 'selectedRecords', records.length);
		if (records.length > 0) {
			Ext.Msg.confirm('Confirm', 'Are you sure?', function(button) {
				if (button == 'yes') {
					me.deleteCustomer(records);
				}
			});
		}
	},

	saveCustomer : function(button) {
		var view = this.getCustomerEdit();
		if (view.edition) {
			this.updateCustomer(this.getCustomerEditForm().getValues(), this.getCustomerEditForm().getRecord());
		} else {
			this.createCustomer(this.getCustomerEditForm().getValues());
		}
		view.close();
	},

	createCustomer : function(values) {
		console.log(this.$className, 'createCustomer', Ext.encode(values));
		this.getCustomersStore().add(values);
		this.getCustomersStore().sync();
	},

	updateCustomer : function(values, record) {
		console.log(this.$className, 'updateCustomer', Ext.encode(values));
		record.set(values);
		this.getCustomersStore().sync();
	},

	deleteCustomer : function(records) {
		console.log(this.$className, 'deleteCustomer', records.length);
		this.getCustomersStore().remove(records);
		this.getCustomersStore().sync();
	},

	searchCustomers : function(button) {
		var extraParams = this.getCustomerSearch().getForm().getValues();
		console.log(this.$className, 'searchCustomers', Ext.encode(extraParams));
		store = this.getCustomersStore();
		store.getProxy().extraParams = extraParams;
		store.load();
	}
});
